package com.fc.app.fcapp;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.baidu.tvsafe.TVSafe;
import com.didi.virtualapk.PluginManager;
import com.fc.app.fcapp.service.Receiver1;
import com.fc.app.fcapp.service.Receiver2;
import com.fc.app.fcapp.service.ResultService;
import com.fc.app.fcapp.service.Service1;
import com.fc.app.fcapp.service.Service2;
import com.fc.app.fcapp.tools.Android;
import com.fc.library.imageloader.ImageLoader;
import com.marswin89.marsdaemon.DaemonApplication;
import com.marswin89.marsdaemon.DaemonConfigurations;

import java.io.DataOutputStream;
import java.io.File;

/**
 * Created by fc on 2017/11/9.
 */

public class FCApplication extends DaemonApplication {

    @Override
    public void onCreate() {
        super.onCreate();
//        ImageLoader.getLoader().init(getApplicationContext());
        mContext = getApplicationContext();
        initResolution(mContext);

        PluginManager pluginManager = PluginManager.getInstance(this);
        //此处是当查看插件apk是否存在,如果存在就去加载(比如修改线上的bug,把插件apk下载到sdcard的根目录下取名为plug.apk)
        File apk = new File(Environment.getExternalStorageDirectory(), "plug.apk");
        if (apk.exists()) {
            try {
                Log.v("virtualapk", "准备加载...");
                pluginManager.loadPlugin(apk);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    "SDcard根目录未检测到plugin.apk插件", Toast.LENGTH_SHORT).show();
        }

//        if (upgradeRootPermission(getPackageCodePath())) {
//            Log.i("baiduResult", "upgradeRootPermission succeed");
//        } else {
//            Log.i("baiduResult", "upgradeRootPermission 失败");
//        }


        //初始化百度sdk
        TVSafe.onApplicationCreate(getApplicationContext(), getApplicationContext().getPackageName(), ResultService.class.getCanonicalName());

    }

    @Override
    public void attachBaseContextByDaemon(Context base) {
        TVSafe.attachBaseContext(base);
        super.attachBaseContextByDaemon(base);
    }

    @Override
    protected DaemonConfigurations getDaemonConfigurations() {

        DaemonConfigurations.DaemonConfiguration configuration1 = new DaemonConfigurations.DaemonConfiguration(
                "com.fc.app.fcapp:process1",
                Service1.class.getCanonicalName(),
                Receiver1.class.getCanonicalName());

        DaemonConfigurations.DaemonConfiguration configuration2 = new DaemonConfigurations.DaemonConfiguration(
                "com.fc.app.fcapp:process2",
                Service2.class.getCanonicalName(),
                Receiver2.class.getCanonicalName());

        DaemonConfigurations.DaemonListener listener = new MyDaemonListener();
        //return new DaemonConfigurations(configuration1, configuration2);//listener can be null
        return new DaemonConfigurations(configuration1, configuration2, listener);
    }


    class MyDaemonListener implements DaemonConfigurations.DaemonListener {
        @Override
        public void onPersistentStart(Context context) {
            Log.i("jincheng", " onPersistentStart");

        }

        @Override
        public void onDaemonAssistantStart(Context context) {
            Log.i("jincheng", " onDaemonAssistantStart");
        }

        @Override
        public void onWatchDaemonDaed() {
            Log.i("jincheng", " onWatchDaemonDaed");
        }
    }

    private static Context mContext = null;

    public static Context getContext() {
        return mContext;
    }


    private static synchronized void initResolution(Context context) {
        int height = Android.getDisplayHeight(context);
        int width = Android.getDisplayWidth(context);
        float density = Android.getDisplayDensity(context);
        float mDiv = (float) width / 1920.0f;
        float mDpi = mDiv / Android.getDisplayDensity(context);
        Log.d("FCApplication", String.format("initResolution %dx%d density:%f div:%f dpi:%f", width, height, density, mDiv, mDpi));
    }



}